package com.xc.xcoj.judge.codesandbox;

import com.github.mustachejava.Code;
import com.xc.xcoj.judge.codesandbox.impl.ExampleCodeSandbox;
import com.xc.xcoj.judge.codesandbox.impl.RemoteCodeSandbox;
import com.xc.xcoj.judge.codesandbox.impl.ThirdPartyCodeSandbox;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码沙箱工厂 （根据字符串参数创建指定的代码沙箱实例）
 * @author zqx
 * @since 2024/10/20
 */
public class CodeSandboxFactory implements InitializingBean {

    @Resource
    private List<CodeSandbox> codeSandboxList;

    private static Map<String, CodeSandbox> codeSandboxMap = new HashMap<>();


    public static CodeSandbox getInstance(String type){
        // 可以获取到 代理的沙箱代码类
        CodeSandbox codeSandbox = codeSandboxMap.get(type);
        return new CodeSandboxProxy(codeSandbox);
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        for (CodeSandbox codeSandbox : codeSandboxList) {
            codeSandboxMap.put(codeSandbox.CodeSandboxType(), codeSandbox);
        }
    }



    /**
     * 创建代码沙箱示例
     *
     * @param type 沙箱类型
     * @return
     */
    public static CodeSandbox newInstance(String type) {
        switch (type) {
            case "example":
                return new ExampleCodeSandbox();
            case "remote":
                return new RemoteCodeSandbox();
            case "thirdParty":
                return new ThirdPartyCodeSandbox();
            default:
                return new ExampleCodeSandbox();
        }
    }

}
